package com.game.centerserver.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.game.centerserver.common.Result;
import com.game.centerserver.infrastructure.entity.User;
import com.game.centerserver.infrastructure.service.UserService;
import com.game.centerserver.service.UserServiceService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: tian
 * @Date: 2024/11/18/16:56
 * @Description:
 */
@Service
public class UserServiceServiceImpl implements UserServiceService {

    @Resource
    private UserService userService;

    @Override
    public Result doLogin(String iphoneNumber, String password) {
        User user = userService.queryByIphoneNumber(iphoneNumber);
        if(user == null){
            return Result.fail("用户不存在，请先注册");
        }
        //密码是使用sha256进行加密的
        if (user.getIphoneNumber().equals(iphoneNumber) &&
                SecureUtil.sha256(password).equals(user.getPassword())){
            StpUtil.login(user.getUid());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            //登录成功后将token返回给前端,无cookie模式下（app，小程序）前端需要获取到token，并手动封装到请求头中
            return Result.ok("登录成功",tokenInfo);
        }
        return Result.fail("登录失败,请检查用户名或密码是否正确");
    }

    @Override
    public Result register(String iphoneNumber, String password) {
        //先判断是否注册过
        User tempUser = userService.queryByIphoneNumber(iphoneNumber);
        if(tempUser != null){
            return Result.fail("已经注册过啦，请不要重复注册");
        }
        User user = new User();
        user.setUid(IdUtil.randomUUID());
        user.setIphoneNumber(iphoneNumber);
        user.setPassword(SecureUtil.sha256(password));
        user.setCreateTime(new Date());
        Boolean insert = userService.insert(user);
        if (!insert){
            return Result.fail("注册失败");
        }
        return Result.ok("注册成功");
    }

    @Override
    public Result logout() {
        StpUtil.logout();
        return Result.ok("退出成功");
    }

    /**
     * 通过uid查询用户
     * @param uid
     * @return
     */
    @Override
    public User queryUserByUid(String uid) {
        return userService.queryById(uid);
    }

    @Override
    public Result deleteUserByUid(String targetUid) {
        if (StpUtil.isLogin()){
            String currentUid = StpUtil.getLoginIdAsString();
            User currentUser = queryUserByUid(currentUid);
            if (currentUser.getIsAdmin() == 1){
                boolean b = userService.deleteById(targetUid);
                if (b){
                    return Result.ok("用户已删除");
                }else {
                    return Result.fail("用户删除失败");
                }
            }else {
                return Result.fail("您没有管理员权限");
            }
        }else {
            return Result.fail("未登录，请先登录");
        }
    }

    @Override
    public boolean isAdmin() {
        String currID = StpUtil.getLoginIdAsString();
        if (currID == null) return false;
        int res = userService.queryById(currID).getIsAdmin();
        return res == 1;
    }
}
